package com.framework.spring.context.bfpp;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.stereotype.Component;

@Component
@Slf4j(topic = "e")
public class D implements BeanDefinitionRegistryPostProcessor {

	public D() {
		log.info("D construct");
	}

	@Override
	public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
		log.info("scan D#BeanDefinitionRegistryPostProcessor.postProcessBeanDefinitionRegistry register E");
		AbstractBeanDefinition eBeanDefinition = BeanDefinitionBuilder.genericBeanDefinition(E.class).getBeanDefinition();
		registry.registerBeanDefinition("e", eBeanDefinition);

		AbstractBeanDefinition fBeanDefinition = BeanDefinitionBuilder.genericBeanDefinition(F.class).getBeanDefinition();
		registry.registerBeanDefinition("f", fBeanDefinition);
//		// 可以执行
//		AbstractBeanDefinition kBeanDefinition = BeanDefinitionBuilder.genericBeanDefinition(K.class).getBeanDefinition();
//		registry.registerBeanDefinition("k", kBeanDefinition);

//		// 执行时机不对 无法执行L的回调
//		AbstractBeanDefinition jBeanDefinition = (AbstractBeanDefinition) registry.getBeanDefinition("j");
//		jBeanDefinition.setBeanClass(L.class);

//		// 此时 x中的@Bean没有生效 因为通过@Component已经初始化
//		AbstractBeanDefinition xBeanDefinition = BeanDefinitionBuilder.genericBeanDefinition(X.class).getBeanDefinition();
//		registry.registerBeanDefinition("x", xBeanDefinition);
	}

	@Override
	public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
		log.info("scan D#BeanDefinitionRegistryPostProcessor.postProcessBeanFactory");
	}

}
